import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Layout from '../views/Layout.vue'
import store from '../store/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'home',
        component: Home
      },
      {
        path: '/topic',
        name: 'topic',
        component: () => import("../views/Topic.vue")  //路由懒加载(异步引入)
      },
      {
        path: '/mine',
        name: 'mine',
        beforeEnter: (to, from, next) => {
          if(store.state.isLogin && store.state.token && localStorage.getItem("token")){
            next()
          }else{
            next('./login')
          }
        },
        component: () => import("../views/Mine.vue")
      },
      {
        path: '/search',
        name: 'search',
        component: () => import("../views/Search.vue")
      }
    ]
  },
  {
    path:"/more",
    name:"More",
    component:() => import("../views/Home/MusicMore.vue")
  },
  {
    path:"/details/:id",
    name:"Details",
    component:() => import("../views/Home/MusicDetails.vue")
  },
  {
    path:"/player/:id/:name/:author/:image",
    name:"Player",
    component:() => import("../views/Player.vue")
  },
  {
    path:"/dj/:id",
    name:"DJMore",
    component:() => import("../views/Home/DJMore.vue")
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/Login.vue")
  }
]

const router = new VueRouter({
  // mode: 'history', // 访问路径不带#号，如：http://localhost:8080/vue，默认是hash模式
  mode: 'hash',
  base: process.env.BASE_URL, // 配置单页应用的基路径，默认是'/',当点击路由时：会带上项目基路径，如:http://localhost:8080/vue/about
  routes,
  linkActiveClass: "active"  //路由激活时动态添加样式
})

export default router
